// pages/pubArticle/pubArticle.js
const app = getApp()
const FormData=require("../../utils/wx-formdata/formData.js")
import {address} from "../../request/api"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: "",
        content: "",
        // fileList: [], //图片存放的数组
        uploadfile:"",
        src: "../../images/icon/添加.png", //图片路径
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    bindtitle(e) {
        console.log(e.detail.value);
        this.setData({
            title: e.detail.value
        })
    },
    bindcontent(e) {
        console.log(e.detail.value);
        this.setData({
            content: e.detail.value
        })
    },
    // 上传图片
    uploadImage: function () {
        var that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                that.upload(that, tempFilePaths);
            }
        })
    },
    //   上传图片
    upload(page, path) {
        var that = this
        wx.showToast({
            title: '正在上传',
            icon: "loading"
        })
        wx.uploadFile({
            filePath: path[0],
            name: 'file',
            url: `http://127.0.0.1:80/user/upload_img` ,
            header: {
                "Content-Type": "multipart/form-data"
            },
            success({
                data: res
            }) {
                console.log(res);
                res = JSON.parse(res)
                if (res.status != 200) return wx.showModal({
                    title: '提示',
                    content: '上传失败',
                    showCancel: false
                })
                let data = res.data
                console.log(data);
                that.setData({ //上传成功修改显示头像
                    src: path[0],
                    uploadfile:path[0]
                })
            },
            fail: function (e) {
                console.log(e);
                wx.showModal({
                    title: '提示',
                    content: '上传失败',
                    showCancel: false
                })
            },
            complete: function () {
                wx.hideToast(); //隐藏Toast
            }

        })
    },
    // 发布文章
    async publishArticle() {
        var that=this
        let formData=new FormData()
        if(!that.data.title||!that.data.content||!that.data.uploadfile) return wx.showToast({
          title: '参数不全',
          icon:"error"
        })
        console.log(app.globalData.bindUserInfo);
        formData.append("id",app.globalData.bindUserInfo.id)
        formData.append("title",that.data.title)
        formData.append("content",that.data.content)
        formData.append("cover",that.data.uploadfile)
        var datas=formData.getData()
        await wx.request({
          url: `${address}/article`,
          method:"POST",
          data:datas.buffer,
          header: {
            'content-type':datas.contentType
          },
          success({data:res}){
            console.log(res);
            if(res.status==200){
                wx.switchTab({
                  url: '../Discuss/Discuss',
                  success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.onLoad();
                  }
                })
            }
          }
        })
    },
    // 取消发布
    Cancel() {
        wx.switchTab({
            url: '../Discuss/Discuss',
            success: function (e) {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onLoad();
              }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})